package com.spa.interfaces.admin;

import com.spa.application.command.admin.MenuAddCommand;
import com.spa.application.command.admin.ResourceAddCommand;
import com.spa.application.command.admin.ResourceEditCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.ResourceDTO;
import com.spa.application.dto.admin.menu.RouterDTO;
import com.spa.application.dto.admin.menu.geeker.ButtonDTO;
import com.spa.application.dto.admin.menu.geeker.MenuDTO;
import com.spa.application.dto.admin.menu.geeker.MenuSelectDTO;
import com.spa.application.query.BaseIdQuery;
import com.spa.application.query.admin.ResourceQuery;
import com.spa.application.service.admin.ResourceApplicationService;
import com.spa.infrastructure.common.ResponseHelper;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.mapper.ResourceMapStruct;
import com.spa.interfaces.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 资源管理控制器.
 *
 * @author liangjun
 */
@RestController
@RequestMapping("admin/resource")
public class ResourceController extends BaseController {

    @Autowired
    private ResourceApplicationService resourceApplicationService;

    @Autowired
    private ResponseHelper responseHelper;

    @PostMapping("addMenu")
    public ResponseMessage<Void> addMenu(@Validated @RequestBody MenuAddCommand menuAddCommand) {
        resourceApplicationService.addMenu(menuAddCommand);
        return responseHelper.success();
    }

    @PostMapping("/menu/list")
    public ResponseMessage<List<MenuDTO>> menuList() {
        return responseHelper.success(resourceApplicationService.menuList());
    }

    @PostMapping("/menu/select/list")
    public ResponseMessage<List<MenuSelectDTO>> menuList4Select() {
        return responseHelper.success(resourceApplicationService.menuList4Select());
    }

    @PostMapping("/menu/page")
    public ResponseMessage<PageResult<MenuDTO>> menuPage() {
        return responseHelper.success(resourceApplicationService.menuList());
    }

    @GetMapping("auth/buttons")
    public ResponseMessage<ButtonDTO> buttonList() {
        ButtonDTO buttonDTO = new ButtonDTO();
        List<String> authButton = new ArrayList<>();
        authButton.add("add");
        authButton.add("edit");
        authButton.add("delete");
        authButton.add("import");
        authButton.add("export");
        List<String> useProTable = new ArrayList<>();
        useProTable.add("add");
        useProTable.add("batchAdd");
        useProTable.add("export");
        useProTable.add("batchDelete");
        useProTable.add("status");
        List<String> demo = new ArrayList<>();
        demo.add("add");
        demo.add("batchAdd");
        demo.add("export");
        demo.add("batchDelete");
        demo.add("status");
        buttonDTO.setUseProTable(useProTable);
        buttonDTO.setAuthButton(authButton);
        buttonDTO.setOrder(demo);
        return responseHelper.success(buttonDTO);
    }

    /**
     * 路由列表
     */
    @PostMapping("/router/list")
    @Deprecated
    public ResponseMessage<List<RouterDTO>> routerList() {
        return responseHelper.success(ResourceMapStruct.INSTANCE.resourceDTOToRouterDTOList(resourceApplicationService.listResourceByUser()));
    }

    /**
     * 查询资源.
     */
    @PostMapping("list")
    @Deprecated
    public ResponseMessage<List<ResourceDTO>> list() {
        return responseHelper.success(resourceApplicationService.listResourceByUser());
    }

    /**
     * 分页查询资源.
     */
    @PostMapping("page")
    @Deprecated
    public ResponseMessage<PageResult<ResourceDTO>> page(@RequestBody ResourceQuery query) {
        return responseHelper.success(resourceApplicationService.page(query));
    }

    /**
     * 新增资源.
     */
    @PostMapping("addResource")
    @Deprecated
    public ResponseMessage<Void> addResource(@Validated @RequestBody ResourceAddCommand resourceAddCommand) {
        resourceApplicationService.add(resourceAddCommand);
        return responseHelper.success();
    }

    /**
     * 修改资源.
     */
    @PostMapping("editResource")
    @Deprecated
    public ResponseMessage<Void> editResource(@Validated @RequestBody ResourceEditCommand resourceEditCommand) {
        resourceApplicationService.edit(resourceEditCommand);
        return responseHelper.success();
    }

    /**
     * 删除资源.
     */
    @PostMapping("delResource")
    @Deprecated
    public ResponseMessage<Void> delResource(@Validated @RequestBody BaseIdQuery idQuery) {
        if(resourceApplicationService.delById(idQuery.getId())){
            return responseHelper.success();
        }
        return responseHelper.fail();
    }

    /**
     * 启用/禁用.
     */
    @PostMapping("enableDisabled")
    @Deprecated
    public ResponseMessage<Void> enableDisabled(@Validated @RequestBody BaseIdQuery idQuery) {
        if(resourceApplicationService.enableDisabled(idQuery.getId())){
            return responseHelper.success();
        }
        return responseHelper.fail();
    }

}
